package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();
        Calendar today = Calendar.getInstance();
        //把时、分、秒、毫秒值全部清零
        today.set(Calendar.HOUR, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        //Calender类的clone()方法可以创建并返回一个Calender对象的副本
        Calendar weekFirstDay = (Calendar) today.clone();
        //设置周一为每周的第一天
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Calendar weekEndDay = (Calendar) today.clone();
        //设置周日为每周的最后一天
        weekEndDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        Calendar monthFirstDay = (Calendar) today.clone();
        //设置 1 号为每月的第一天
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        Calendar monthEndDay = (Calendar) today.clone();
        //Calender的getActualMaximum()方法根据传进去的字段自己进行判断然后获取该字段的最大值
        //把获取到的当前月份的最大值设置为每月的最后一天
        monthEndDay.set(Calendar.DAY_OF_MONTH, monthEndDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("reportDate", simpleDateFormat.format(today.getTime()));
        map.put("todayVisitsNumber", orderDao.selectOrderByOrderTime(today.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        map.put("thisWeekVisitsNumber", orderDao.selectOrderByOrderTime(weekFirstDay.getTime(), weekEndDay.getTime(), Order.ORDERSTATUS_YES));
        map.put("thisMonthVisitsNumber", orderDao.selectOrderByOrderTime(monthFirstDay.getTime(), monthEndDay.getTime(), Order.ORDERSTATUS_YES));
        map.put("todayOrderNumber", orderDao.selectOrderByOrderTime(today.getTime(), today.getTime(), null));
        map.put("thisWeekOrderNumber", orderDao.selectOrderByOrderTime(weekFirstDay.getTime(), weekEndDay.getTime(), null));
        map.put("thisMonthOrderNumber", orderDao.selectOrderByOrderTime(monthFirstDay.getTime(), monthEndDay.getTime(), null));
        map.put("todayNewMember", memberDao.selectByRegTime(today.getTime(), today.getTime()));
        map.put("thisWeekNewMember", memberDao.selectByRegTime(weekFirstDay.getTime(), weekEndDay.getTime()));
        map.put("thisMonthNewMember", memberDao.selectByRegTime(monthFirstDay.getTime(), monthEndDay.getTime()));
        map.put("totalMember", memberDao.selectByRegTimeBefore(simpleDateFormat.format(today.getTime())));
        map.put("hotSetmeal", orderDao.selectHotSetmeal());

        return map;
    }

    @Override
    public List<Integer> getMemberCountByMonths(List<String> months) {
        List<Integer> countList = new ArrayList<>();
        //遍历去年一年的月份
        for (String regTime : months) {
            //对当前时间的年月按" . "进行切割
            String[] split = regTime.split("\\.");
            String year = "";
            String month = "";
            //遍历切割后的数组
            for (String s : split) {
                //判断当前字符串的长度是否 > 2 ，>2就是年份
                if (s.length() > 2) {
                    year = s;
                }
                month = s;
            }
            //判断年份是否是闰年、月份是不是2月，符合条件就拼接当前月份的最大天数（以下if语句同理）
            if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(month) == 2) {
                regTime = regTime + ".29";
            }
            if (Integer.parseInt(year) % 4 != 0 && Integer.parseInt(month) == 2) {
                regTime = regTime + ".28";
            }
            if (Integer.parseInt(month) == 1 || Integer.parseInt(month) == 3 || Integer.parseInt(month) == 5 || Integer.parseInt(month) == 7 || Integer.parseInt(month) == 8 || Integer.parseInt(month) == 10 || Integer.parseInt(month) == 12) {
                regTime = regTime + ".31";
            }
            if(Integer.parseInt(month) == 4||Integer.parseInt(month) == 6||Integer.parseInt(month) == 9||Integer.parseInt(month) == 11) {
                regTime = regTime + ".30";
            }
            //String s = regTime + ".31";
            Integer count = memberDao.selectByRegTimeBefore(regTime);
            countList.add(count);
        }
        return countList;
    }
}
